package com.java.util;



import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

@Slf4j
public class InitialUtil {
	public static final String STR_F = "f";
	public static final String STR_T = "t";
	private static final String ak = "";// 百度地图经纬度反显秘钥
	public static HttpClient httpClient = new HttpClient();

	/** 查询百度接口地址转经�? */
	public static String loadJSON(String url) {
		StringBuilder json = new StringBuilder();
		try {
			URL oracle = new URL(url);
			URLConnection yc = oracle.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
			String inputLine = null;
			while ((inputLine = in.readLine()) != null) {
				json.append(inputLine);
			}
			in.close();
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		}
		return json.toString();
	}

	/**
	 * 获取百度接口地址转经纬度
	 */
	public static String getLngAndLat(String address) {
		String json = "";
		try {
			String url = "http://api.map.baidu.com/geocoder/v2/?address=" + address + "&city=北京�?&output=json&ak=" + ak;
			json = loadJSON(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 获取百度地图的经纬度
	 *
	 * @param address
	 */
	public static String getcoorder(String address) {
		// https://jingweidu.51240.com/
		HttpClient httpClient = new HttpClient();
		try {
			String urlString = "https://apis.map.qq.com/jsapi?qt=geoc&addr=" + getURLEncoderString(address);
			GetMethod post = new GetMethod(urlString);
			String randomIp = getRandomIp();
			post.setRequestHeader("X-Real-IP", randomIp);
			post.setRequestHeader("X-Forwarded-For", randomIp);
			post.setRequestHeader("Proxy-Client-IP", randomIp);
			post.setRequestHeader("WL-Proxy-Client-IP", randomIp);
			post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); // 设置编码
			httpClient.executeMethod(post);
			JSONObject json = JSONObject.fromObject(post.getResponseBodyAsString());
			JSONObject jsono = json.getJSONObject("detail");
			String s = map_tx2bd(Double.valueOf(jsono.get("pointx").toString()), Double.valueOf(jsono.get("pointy").toString()));
			System.out.println(address+": 经纬度为:"+s);
			return s;
		} catch (Exception e) {
			System.out.println(address+": 没有获取到该地址的经纬度 ...");
		}
		return null;
	}

	/**
	 * 坐标转换，腾讯地图转换成百度地图坐标
	 *
	 * @param lat
	 *            腾讯纬度
	 * @param lon
	 *            腾讯经度
	 * @return 返回结果：经度，纬度
	 */
	public static String map_tx2bd(double lon, double lat) {
		double bd_lat;// 纬度
		double bd_lon;// 经度
		double x_pi = 3.14159265358979324;
		double x = lon, y = lat;
		double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
		bd_lon = z * Math.cos(theta) + 0.0065;
		bd_lat = z * Math.sin(theta) + 0.006;
		return bd_lon + "  " + bd_lat;
	}

	/**
	 * 坐标转换，百度地图坐标转换成腾讯地图坐标
	 *
	 * @param lat
	 *            百度坐标纬度
	 * @param lon
	 *            百度坐标经度
	 * @return 返回结果：经度，纬度
	 */
	public String map_bd2tx(double lat, double lon) {
		double tx_lat;
		double tx_lon;
		double x_pi = 3.14159265358979324;
		double x = lon - 0.0065, y = lat - 0.006;
		double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
		tx_lon = z * Math.cos(theta);
		tx_lat = z * Math.sin(theta);
		return tx_lat + "," + tx_lon;
	}

	private final static String ENCODE = "utf-8";

	/**
	 * URL 转码
	 *
	 * @return String
	 * @date 2015-3-17 下午04:10:28
	 */
	public static String getURLEncoderString(String str) {
		String result = "";
		if (null == str) {
			return "";
		}
		try {
			result = java.net.URLEncoder.encode(str, ENCODE);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 生成随机ip数据
	 *
	 * @return
	 */
	public static String getRandomIp() {
		// ip范围
		int[][] range = { { 607649792, 608174079 }, // 36.56.0.0-36.63.255.255
				{ 1038614528, 1039007743 }, // 61.232.0.0-61.237.255.255
				{ 1783627776, 1784676351 }, // 106.80.0.0-106.95.255.255
				{ 2035023872, 2035154943 }, // 121.76.0.0-121.77.255.255
				{ 2078801920, 2079064063 }, // 123.232.0.0-123.235.255.255
				{ -1950089216, -1948778497 }, // 139.196.0.0-139.215.255.255
				{ -1425539072, -1425014785 }, // 171.8.0.0-171.15.255.255
				{ -1236271104, -1235419137 }, // 182.80.0.0-182.92.255.255
				{ -770113536, -768606209 }, // 210.25.0.0-210.47.255.255
				{ -569376768, -564133889 }, // 222.16.0.0-222.95.255.255
		};

		Random rdint = new Random();
		int index = rdint.nextInt(10);
		String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0]));
		return ip;
	}

	/*
	 * 将十进制转换成IP地址
	 */
	public static String num2ip(int ip) {
		int[] b = new int[4];
		String x = "";
		b[0] = (int) ((ip >> 24) & 0xff);
		b[1] = (int) ((ip >> 16) & 0xff);
		b[2] = (int) ((ip >> 8) & 0xff);
		b[3] = (int) (ip & 0xff);
		x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);
		return x;
	}


	public static String getLatAndLong(){

		try {
			String address = getURLEncoderString("北京市海淀区有福敬老院");
			String urlString = "https://apis.map.qq.com/jsapi?qt=geoc&addr=" + address;
			GetMethod post = new GetMethod(urlString);
			String randomIp = getRandomIp();
			post.setRequestHeader("X-Real-IP", randomIp);
			post.setRequestHeader("X-Forwarded-For", randomIp);
			post.setRequestHeader("Proxy-Client-IP", randomIp);
			post.setRequestHeader("WL-Proxy-Client-IP", randomIp);
			post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); // 设置编码
			httpClient.executeMethod(post);
			// System.out.println(post.getResponseBodyAsString());
			JSONObject json = JSONObject.fromObject(post.getResponseBodyAsString());// new
			// JSONObject();
			JSONObject jsono = json.getJSONObject("detail");
			System.out.println(json);
			System.out.println("解析出来的经度：" + String.format("%.7f", Double.parseDouble(jsono.get("pointx").toString())));
			System.out.println("解析出来的纬度：" + String.format("%.7f", Double.parseDouble(jsono.get("pointy").toString())));
			System.out.println("转换成百度经纬度?" + map_tx2bd(Double.valueOf(jsono.get("pointx").toString()), Double.valueOf(jsono.get("pointy").toString())));
		    return map_tx2bd(Double.valueOf(jsono.get("pointx").toString()), Double.valueOf(jsono.get("pointy").toString()));
		} catch (Exception e) {

			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		System.out.println(InitialUtil.getcoorder("地址：市北区-十五中上清路"));
	}

}
